﻿using Microsoft.AspNetCore.Mvc;
using YL.NetCore.NetCoreApp;
using YL.Utils.Pub;
using YL.Utils.Table;

using IServices;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EduCMS.Controllers
{
    [Route("[controller]")]
    public class ArticleController : BaseController
    {
        private readonly IWhr_contentServices _services;
        private readonly IWhr_minio_fileServices _fileServices;
        public ArticleController(IWhr_contentServices services, IWhr_minio_fileServices fileServices) 
        {
            _services = services;
            _fileServices = fileServices;
        }

        /// <summary>
        /// 无需登录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}"), AllowAnonymous]
        public JsonResult Detail(int id) 
        {
            var articleInfo = _services.QueryableToEntity(c => c.Id == id);
            var code = articleInfo != null ? HttpStatus.status200 : HttpStatus.status404;
            return Json(Microprogram.DataInfo(PubEnum.Success, "", articleInfo));
        }

        /// <summary>
        /// 广告列表，无需登录
        /// </summary>
        /// <returns></returns>
        [HttpGet("ad"), AllowAnonymous]
        public IActionResult GetAd() 
        {
            var result = _services.GetAd();
            var output = new List<object>();
            foreach (var res in result)
            {
                output.Add(new
                {
                    res.Title,
                    res.Url,
                    res.Img
                });
            }
            return Json(Microprogram.DataInfo(PubEnum.Success, "", output));
        }
    }
}